﻿/*
 * Created by SharpDevelop.
 * User: Jobe
 * Date: 30.01.2007
 * Time: 22:36
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Roster
{
	/// <summary>
	/// Description of Roster.
	/// </summary>
	public class Roster
	{
		
		public delegate void ProgressReport( byte[] Buffer, long Total, long Written );
		
		public string URL = null;
		
		public Roster(string URL)
		{
			this.URL = URL;
		}
		
		public void Upload(Hashtable svFiles, ProgressReport Report ){
			foreach(DictionaryEntry de in svFiles){
				this.Upload((string)de.Value, (string)de.Key, Report);
			}
		}
		
		public void Upload(string Filename, string fileFormName, ProgressReport Report ){
			
			FileInfo upFile = new FileInfo(Filename);
			
			if(!upFile.Exists)throw new Exception(upFile.FullName + " does not exits!");
			
			// HttpWebRequest mit Boundary
	        string boundary;
	      
	        StringBuilder sb = new StringBuilder();
	
	        boundary = "----------" + DateTime.Now.Ticks.ToString("x");
	
	        // 1. Request generieren
	        HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(this.URL);
	
	        // 2. Content type, Methode und User Agent setzen
	        hwr.ContentType = "multipart/form-data; boundary=" + boundary;
	        hwr.Method = "POST";
	        hwr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
	
	        // 3. Build up the post message header
	        sb.Append("--");
	        sb.Append(boundary);
	        sb.Append("\r\n");
	        sb.Append("Content-Disposition: form-data; name=\"");
	        sb.Append(fileFormName);
	        sb.Append("\"; filename=\"");
	        sb.Append(upFile.Name);
	        sb.Append("\"");
	        sb.Append("\r\n");
	        sb.Append("Content-Type: ");
	        sb.Append("text/xml");
	        sb.Append("\r\n");
	        sb.Append("\r\n");
	
	        string postHeader = sb.ToString();
	        byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);

	        // 4. File Buffer / Header bilden
	        byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
	        FileStream fileStream = new FileStream(upFile.FullName, FileMode.Open, FileAccess.Read);
	        long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
	        hwr.ContentLength = length;
	        Stream requestStream = hwr.GetRequestStream();
	
	        // 5. Write out our post header
	        requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
	
	        // 6. Write out the file contents
	        byte[] buffer = new Byte[checked((uint)Math.Min(4096,
	                                 (int)fileStream.Length))];
	        int bytesRead = 0;
	        long bytesTotal = 0;
	        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0){
	            requestStream.Write(buffer, 0, bytesRead);
				bytesTotal += bytesRead;
				Report.Invoke(buffer,length,bytesTotal);
				Application.DoEvents();// do not block app
	        }
	
	        // 7. Write out the trailing boundary
	        requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
	        
	        // 8. Close Streams
	        fileStream.Close();
	        requestStream.Close();
	        
	        if(hwr.HaveResponse){
	        	
	        	HttpWebResponse Response = (HttpWebResponse)hwr.GetResponse();
			    Stream StreamResponse = Response.GetResponseStream();
			    FileStream StreamFile = new FileStream( Application.StartupPath + "/response.html", FileMode.CreateNew );
			    BinaryReader br = new BinaryReader(StreamResponse);
			    BinaryWriter bw = new BinaryWriter(StreamFile);
				bw.Write(br.ReadBytes((int)StreamResponse.Length));

	        }
	        
		}
		
	}
}
